﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Buy2Site.UserControls.ShoppingCart;
using Buy2Site.Classes;

namespace Buy2Site.UserControls
{
    public partial class ShoppingCartUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ListOfItems"] != null)
            {
                List<ProductClass> list = ((ProductClass)Session["ListOfItems"]).ListOfItems;
                foreach (var item in list)
                {
                    ShoppingCartItem shoppingCart = (ShoppingCartItem)LoadControl("~/UserControls/ShoppingCart/ShoppingCartItem.ascx");
                    ShoppingCartItem1.Controls.Add(shoppingCart);
                    //ShoppingCartItem1.

                    // TODO: add variables for the product details
                    // TODO: each link should be with variables to the product details

                    // chkbItem.Text = "<a href='~/Pages/ProductDetailsPage.aspx'>Product Details</a>";
                }
                ShoppingCartTotalPrice totalPriceUC = (ShoppingCartTotalPrice)LoadControl("~/UserControls/ShoppingCart/ShoppingCartTotalPrice.ascx");
                ShoppingCartTotalPrice1.Controls.Add(totalPriceUC);

                //ShoppingCartItem
            }
        }

        protected void btnBuy_Click(object sender, EventArgs e)
        {
            // set all products as sold - add label near each one
            ShoppingCartItem.Sold = "Sold!";
            //((ProductClass)Application["ListOfAllQuantetiesItems"]).ListOfAllQuantetiesItems
            // show thank you message
            ClientScriptManager scriptManage = Page.ClientScript;
            string strConfirm = "<script>if(!window.confirm('Thank you for buying on our site'))" +
                                "{window.location.href='~/Pages/Home.aspx'}</script>";
            scriptManage.RegisterClientScriptBlock(this.GetType(), "Confirm", strConfirm, false);
            // redirect to main page
            Response.Redirect("HomePage.aspx"); // ~/Pages/HomePage.aspx
        }
    }
}